import time
import uuid

import allure
import pytest
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


# 定义一个 session 范围的 fixture
@pytest.fixture(scope="session", autouse=True)
def setup_driver():
    driver = webdriver.Firefox()
    # 打开litemall登录页面
    driver.get('https://litemall.hogwarts.ceshiren.com/#/login')
    # 清空账号  输入账号
    time.sleep(1)
    name = driver.find_element(By.NAME, 'username')
    name.clear()
    name.send_keys("manage")
    # 清空密码  输入密码
    time.sleep(1)
    password = driver.find_element(By.NAME, "password")
    password.clear()
    password.send_keys('manage123')
    # 点击登录按钮
    time.sleep(1)
    driver.find_element(By.CLASS_NAME, 'el-button--mini').click()
    # 进入系统管理模块子页面
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR, ".menu-wrapper:nth-child(5) .el-submenu__title").click()
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR, ".is-opened .el-menu-item").click()

    yield driver

    # 关闭当前窗口
    time.sleep(1)
    driver.close()
    # 关闭浏览器
    driver.quit()


@allure.step
def first_step(setup_driver):
    time.sleep(5)
    print("步骤0：清除name,id,重新搜索")
    # 清空name
    WebDriverWait(setup_driver, 10).until(
        expected_conditions.element_to_be_clickable(
            (By.CSS_SELECTOR, '.filter-item:nth-child(2) > .el-input__inner')))
    name = setup_driver.find_element(By.CSS_SELECTOR, ".filter-item:nth-child(2) > .el-input__inner")
    name.send_keys(Keys.CONTROL, "a")  # 相当于ctrl + a快捷键全选
    name.send_keys(Keys.DELETE)  # 快捷键删除
    # 清空id
    WebDriverWait(setup_driver, 10).until(
        expected_conditions.element_to_be_clickable(
            (By.CSS_SELECTOR, '.filter-item:nth-child(1) > .el-input__inner')))
    idd = setup_driver.find_element(By.CSS_SELECTOR, ".filter-item:nth-child(1) > .el-input__inner")
    idd.send_keys(Keys.CONTROL, "a")  # 相当于ctrl + a快捷键全选
    idd.send_keys(Keys.DELETE)  # 快捷键删除
    # 点击搜索按钮
    WebDriverWait(setup_driver, 10).until(
        expected_conditions.element_to_be_clickable(
            (By.CSS_SELECTOR, '.el-button:nth-child(3)')))
    setup_driver.find_element(By.CSS_SELECTOR, ".el-button:nth-child(3)").click()
    WebDriverWait(setup_driver, 10).until(
        expected_conditions.visibility_of_element_located(
            (By.CSS_SELECTOR, '.el-table')))


@allure.title("测试增加对象")
@allure.description("测试增加对象：先删除名字相同的对象，再新增对象，搜索新增的对象进行验证")
def test_add(setup_driver):
    first_step(setup_driver)
    with allure.step("步骤1：清除新增前名字相同的对象"):
        # 清除新增前名字相同的对象
        time.sleep(1)
        name = setup_driver.find_element(By.CSS_SELECTOR, ".filter-item:nth-child(2) > .el-input__inner")
        text = "oppo.png"
        for char in text:
            name.send_keys(char)
        time.sleep(1)
        setup_driver.find_element(By.CSS_SELECTOR, ".el-button:nth-child(3)").click()
        time.sleep(3)
        elements = setup_driver.find_elements(By.CSS_SELECTOR, ".el-table__row")
        while elements.__len__() != 0:
            time.sleep(1)
            setup_driver.find_element(By.CSS_SELECTOR,
                                      ".el-table__row:nth-child(1)>td:nth-child(7)>div>button:nth-child(2)").click()
            time.sleep(1)
            name = setup_driver.find_element(By.CSS_SELECTOR, ".filter-item:nth-child(2) > .el-input__inner")
            name.send_keys(Keys.CONTROL, "a")
            name.send_keys(Keys.DELETE)
            time.sleep(1)
            text = "oppo.png"
            for char in text:
                name.send_keys(char)
            time.sleep(1)
            setup_driver.find_element(By.CSS_SELECTOR, ".el-button:nth-child(3)").click()
            time.sleep(3)
            elements = setup_driver.find_elements(By.CSS_SELECTOR, ".el-table__row")
        print("清除完毕")
    with allure.step("步骤2：新增对象"):
        # 点击新增按钮
        time.sleep(1)
        setup_driver.find_element(By.CSS_SELECTOR, ".el-button:nth-child(4)").click()
        print("点击新增按钮完毕")
        # 上传文件
        time.sleep(1)
        setup_driver.find_element(By.CSS_SELECTOR, ".el-dialog__body>div>div>.el-upload__input").send_keys(
            r"D:\emos\PycharmProjects\lite\test-image\oppo.png")
        print("上传完毕")
    with allure.step("步骤3：搜索新增后的对象"):
        # 搜索新增后的对象
        time.sleep(3)
        name = setup_driver.find_element(By.CSS_SELECTOR, ".filter-item:nth-child(2) > .el-input__inner")
        name.send_keys(Keys.CONTROL, "a")
        name.send_keys(Keys.DELETE)
        time.sleep(1)
        text = "oppo.png"
        for char in text:
            name.send_keys(char)
        time.sleep(1)
        setup_driver.find_element(By.CSS_SELECTOR, ".el-button:nth-child(3)").click()
        print("搜索完毕")
    with allure.step("步骤4：验证是否新增成功"):
        # 查找是否存在该元素
        time.sleep(1)
        assert setup_driver.find_elements(By.CSS_SELECTOR, ".el-table__row").__len__() == 1


@allure.title("测试删除对象")
@allure.description("测试删除对象：先删除第一个对象，再搜索已删除的对象应该为空")
def test_del(setup_driver):
    first_step(setup_driver)
    with allure.step("步骤1：删除列表第一个对象"):
        # 删除列表第一个对象
        time.sleep(3)
        idd = str(
            setup_driver.find_element(By.CSS_SELECTOR, ".el-table__row:nth-child(1)>.el-table_1_column_1>div").text)
        time.sleep(1)
        setup_driver.find_element(By.CSS_SELECTOR,
                                  ".el-table__row:nth-child(1)>td:nth-child(7)>div>button:nth-child(2)").click()
        print("删除完毕")
    with allure.step("步骤2：查找是否存在该元素"):
        # 查找是否存在该元素
        # 点击id输入框
        time.sleep(1)
        id_text = setup_driver.find_element(By.CSS_SELECTOR, ".filter-item:nth-child(1) > .el-input__inner")
        id_text.send_keys(Keys.CONTROL, "a")
        id_text.send_keys(Keys.DELETE)
        # 输入对应id
        time.sleep(1)
        for char in idd:
            id_text.send_keys(char)
        # 点击搜索按钮
        time.sleep(1)
        setup_driver.find_element(By.CSS_SELECTOR, ".el-button:nth-child(3)").click()
        print("搜索完毕")
    with allure.step("步骤3：验证删除是否成功"):
        # 查找是否存在该元素
        time.sleep(1)
        assert setup_driver.find_elements(By.CSS_SELECTOR, ".el-table__row").__len__() == 0


@allure.title("测试根据名字搜索对象")
@allure.description("测试根据名字搜索对象")
def test_select_by_name(setup_driver):
    first_step(setup_driver)
    with allure.step("步骤1：获取列表第一项name"):
        time.sleep(1)
        # 获取列表第一项name
        name = str(setup_driver.find_element(By.CSS_SELECTOR,
                                             ".el-table__row:nth-child(1)>.el-table_1_column_2>div").text)
        print("name:" + name)
    with allure.step("步骤2：点击名字输入框"):
        # 点击名字输入框
        time.sleep(1)
        name_text = setup_driver.find_element(By.CSS_SELECTOR, ".filter-item:nth-child(2) > .el-input__inner")
        name_text.click()
        print("点击完毕")
    with allure.step("步骤3：输入对应的名字"):
        # 输入对应的名字
        time.sleep(1)
        for char in name:
            name_text.send_keys(char)
        print("输入完毕")
    with allure.step("步骤4：点击搜索按钮"):
        # 点击搜索按钮
        time.sleep(1)
        setup_driver.find_element(By.CSS_SELECTOR, ".el-button:nth-child(3)").click()
        print("搜索完毕")
    with allure.step("步骤5：验证是否存在该元素"):
        # 查找是否存在该元素
        time.sleep(1)
        assert setup_driver.find_elements(By.CSS_SELECTOR, ".el-table__row").__len__() != 0


@allure.title("测试根据id搜索对象")
@allure.description("测试根据id搜索对象")
def test_select_by_id(setup_driver):
    first_step(setup_driver)
    with allure.step("步骤1：获取列表第一项id"):
        time.sleep(1)
        # 获取列表第一项id
        idd = str(
            setup_driver.find_element(By.CSS_SELECTOR, ".el-table__row:nth-child(1)>.el-table_1_column_1>div").text)
        print("id:" + idd)
    with allure.step("步骤2：点击id输入框"):
        # 点击id输入框
        time.sleep(1)
        id_text = setup_driver.find_element(By.CSS_SELECTOR, ".filter-item:nth-child(1) > .el-input__inner")
        id_text.click()
        print("点击完毕")
    with allure.step("步骤3：输入对应id"):
        # 输入对应id
        time.sleep(1)
        for char in idd:
            id_text.send_keys(char)
        print("输入完毕")
    with allure.step("步骤4：点击搜索按钮"):
        # 点击搜索按钮
        time.sleep(1)
        setup_driver.find_element(By.CSS_SELECTOR, ".el-button:nth-child(3)").click()
        print("搜索完毕")
    with allure.step("步骤5：验证是否存在该元素"):
        # 查找是否存在该元素
        time.sleep(1)
        assert setup_driver.find_elements(By.CSS_SELECTOR, ".el-table__row").__len__() == 1


@allure.title("测试根据id和名字搜索对象")
@allure.description("测试根据id和名字搜索对象")
def test_select_by_id_and_name(setup_driver):
    first_step(setup_driver)
    with allure.step("步骤1：获取列表第一项id"):
        time.sleep(1)
        # 获取列表第一项id
        idd = str(
            setup_driver.find_element(By.CSS_SELECTOR, ".el-table__row:nth-child(1)>.el-table_1_column_1>div").text)
        time.sleep(1)
        print("id:" + idd)
    with allure.step("步骤2：获取列表第一项name"):
        # 获取列表第一项name
        name = str(setup_driver.find_element(By.CSS_SELECTOR,
                                             ".el-table__row:nth-child(1)>.el-table_1_column_2>div").text)
        print("name:" + name)
    with allure.step("步骤3：点击id输入框"):
        # 点击id输入框
        time.sleep(1)
        id_text = setup_driver.find_element(By.CSS_SELECTOR, ".filter-item:nth-child(1) > .el-input__inner")
        id_text.click()
        print("点击完毕")
    with allure.step("步骤4：输入对应id"):
        # 输入对应id
        time.sleep(1)
        for char in idd:
            id_text.send_keys(char)
        print("输入完毕")
    with allure.step("步骤5：点击名字输入框"):
        # 点击名字输入框
        time.sleep(1)
        name_text = setup_driver.find_element(By.CSS_SELECTOR, ".filter-item:nth-child(2) > .el-input__inner")
        name_text.click()
        print("点击完毕")
    with allure.step("步骤6：输入对应的名字"):
        # 输入对应的名字
        time.sleep(1)
        for char in name:
            name_text.send_keys(char)
        print("输入完毕")
    with allure.step("步骤7：点击搜索按钮"):
        # 点击搜索按钮
        time.sleep(1)
        setup_driver.find_element(By.CSS_SELECTOR, ".el-button:nth-child(3)").click()
        print("搜索完毕")
    with allure.step("步骤8：验证是否存在该元素"):
        # 查找是否存在该元素
        time.sleep(1)
        assert setup_driver.find_elements(By.CSS_SELECTOR, ".el-table__row").__len__() == 1


@allure.title("测试编辑对象")
@allure.description("测试编辑对象：先生成uuid新名字，再用新名字命名，之后有新名字搜索是否存在验证编辑是否成功")
def test_edit(setup_driver):
    first_step(setup_driver)
    with allure.step("步骤1：点击列表第一个编辑按钮"):
        # 点击列表第一个编辑按钮
        time.sleep(1)
        setup_driver.find_element(By.CSS_SELECTOR, ".el-table__row:nth-child(1) .el-button--primary").click()
        print("点击完毕")
    with allure.step("步骤2：清除原名"):
        # 清除原名
        time.sleep(1)
        name = setup_driver.find_element(By.CSS_SELECTOR, ".el-form-item__content .el-input__inner")
        name.send_keys(Keys.CONTROL, "a")
        name.send_keys(Keys.DELETE)
        print("清除完毕")
    with allure.step("步骤3：生成一个随机的UUID"):
        # 生成一个随机的UUID
        random_uuid = uuid.uuid4()
        # 将UUID转换为字符串
        uuid_string = str(random_uuid)
        print("uuid:" + uuid_string)
    with allure.step("步骤4：输入新名"):
        # 输入新名
        time.sleep(1)
        new_name_text = setup_driver.find_element(By.CSS_SELECTOR, ".el-form-item__content .el-input__inner")
        for char in uuid_string:
            new_name_text.send_keys(char)
        print("输入完毕")
    with allure.step("步骤5：点击确定"):
        # 点击确定
        time.sleep(1)
        setup_driver.find_element(By.CSS_SELECTOR, ".el-button--primary:nth-child(2)").click()
        print("确定完毕")
    with allure.step("步骤6：用修改后的名字搜索对象"):
        # 用修改后的名字搜索对象
        time.sleep(3)
        name = setup_driver.find_element(By.CSS_SELECTOR, ".filter-item:nth-child(2) > .el-input__inner")
        name.send_keys(Keys.CONTROL, "a")
        name.send_keys(Keys.DELETE)
        time.sleep(1)
        for char in uuid_string:
            name.send_keys(char)
        time.sleep(1)
        setup_driver.find_element(By.CSS_SELECTOR, ".el-button:nth-child(3)").click()
        print("搜索完毕")
    with allure.step("步骤1：获取列表第一项id"):
        # 查找是否存在该元素
        time.sleep(1)
        assert setup_driver.find_elements(By.CSS_SELECTOR, ".el-table__row").__len__() == 1


if __name__ == '__main__':
    pytest.main()
